import minio
import os

from configs import configs_service
from minio.deleteobjects import DeleteObject

ak = os.environ['MINIO_AK']
sk = os.environ['MINIO_SK']
client = minio.Minio(
    # endpoint='192.168.11.153:9000',
    endpoint=configs_service.get_config('oss_endpoint'),
    access_key=ak,
    secret_key=sk,
    secure=False
)

def upload_object(file_name, data, length, bucket_name):
    found = client.bucket_exists(bucket_name)
    if not found:
        client.make_bucket(bucket_name)
    result = client.put_object(
        bucket_name=bucket_name, object_name=file_name, data=data, length=length,
    )

def upload_file(file_path, file_name, bucket_name):
    found = client.bucket_exists(bucket_name)
    if not found:
        client.make_bucket(bucket_name)
    result = client.fput_object(
        bucket_name, file_name, file_path,
    )
    # data = client.get_object(bucket_name, file_name)
    # return data
    # with open(file_path, "rb") as file_data:
    #     bytes_length = os.path.getsize(file_path)
    #     # client.put_object(bucket_name, file_path, file_data, bytes_length)


def delete_files(file_names, bucket_name):
    delete_objects = []
    for file_name in file_names:
        delete_objects.append(DeleteObject(file_name))
    errors = client.remove_objects(
        bucket_name=bucket_name,
        delete_object_list=delete_objects
    )
    for error in errors:
        print("error occurred when deleting object", error)


def delete_bucket(bucket_name):
    client.remove_bucket(bucket_name)

def get_object(bucket_name, filename):
    return client.get_object(bucket_name, filename)

def list_objects(bucket_name):
    return client.list_objects(bucket_name=bucket_name)

if __name__ == '__main__':
    upload_file(file_path="./audio_temp/1_1.mp3", file_name="1_2.mp4",
                           bucket_name="video-1-1-1")
    # get_task_result("66d07526da83e70001703dfc")
